package com.egao.api.controller.nont;


import cn.hutool.core.date.DateUnit;
import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.utils.CacheUtils;
import com.egao.common.core.damon.utils.PhoneUtils;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.service.EmailService;
import com.egao.common.system.service.OperRecordService;
import com.egao.common.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 */
@RequestMapping("/api/nt/message")
@RestController
@Api(value = "短信API",tags = {"短信API"})
public class ApiMessagesNtController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private EmailService emailService;
    @Autowired
    private OperRecordService operRecordService;

    /**
     * 发送验证码
     */
    @PostMapping("/phoneMessage")
    @ApiOperation(value = "获取验证码",notes = "phoneNumber 手机号码",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber",value = "手机",paramType = "body",dataType = "String",required = true),
    })
    public JsonResult phoneMessage(String phoneNumber, HttpServletRequest request) throws Exception {
        String random = (int)((Math.random()*9+1)*1000) + "";
        PhoneUtils.sendMsg(phoneNumber, "596942", new String[]{random},1);
        //加入元素，每个元素可以设置其过期时长，DateUnit.SECOND.getMillis()代表每秒对应的毫秒数，在此为秒
        CacheUtils.timedCache.put(phoneNumber, random, DateUnit.SECOND.getMillis() * UserConstant.MESSAGES_SECOND);//10分钟
        return JsonResult.ok("验证码").put("data", random);
    }

    /**
     * 发送验证码
     */
    @PostMapping("/phoneVerify")
    @ApiOperation(value = "验证码是否正确",notes = "" +
            "phoneNumber 手机号码\n" +
            "code 验证码\n" +
            "",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber",value = "手机",paramType = "body",dataType = "String",required = true),
    })
    public JsonResult phoneVerify(String phoneNumber, String code, HttpServletRequest request) throws Exception {
        String random2 = CacheUtils.timedCache.get(phoneNumber);
        if (!code.equals(random2)) {
            return JsonResult.error("验证码不正确");
        }
        return JsonResult.ok("验证码正确");
    }

    /**
     * 发送验证码
     */
    @PostMapping("/emailMessage")
    @ApiOperation(value = "获取验证码",notes = "email ",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email",value = "email",paramType = "body",dataType = "String",required = true),
    })
    public JsonResult emailMessage(String email, HttpServletRequest request) throws Exception {
        String random = (int)((Math.random()*9+1)*1000) + "";
        emailService.sendTextEmail("甩手掌柜验证码","您的验证码为:"+random,new String[]{email});
        //加入元素，每个元素可以设置其过期时长，DateUnit.SECOND.getMillis()代表每秒对应的毫秒数，在此为秒
        CacheUtils.timedCache.put(email, random, DateUnit.SECOND.getMillis() * UserConstant.MESSAGES_SECOND);//10分钟
        return JsonResult.ok("验证码").put("data", random);
    }

    /**
     * 发送验证码
     */
    @PostMapping("/emailVerify")
    @ApiOperation(value = "验证码是否正确",notes = "" +
            "email 验证码\n" +
            "code 验证码\n" +
            "",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email",value = "email",paramType = "body",dataType = "String",required = true),
    })
    public JsonResult emailVerify(String email, String code, HttpServletRequest request) throws Exception {
        String random2 = CacheUtils.timedCache.get(email);
        if (!code.equals(random2)) {
            return JsonResult.error("验证码不正确");
        }
        return JsonResult.ok("验证码正确");
    }

    /**
     * 确认验证码
     */
    @PostMapping("/testMessageVerify")
    public JsonResult testMessageVerify(String phoneNumber, String messageCode, HttpServletRequest request) throws Exception {
        //加入元素，每个元素可以设置其过期时长，DateUnit.SECOND.getMillis()代表每秒对应的毫秒数，在此为秒
        String messageCode2 = CacheUtils.timedCache.get(phoneNumber);
        return JsonResult.ok("验证码").put("messageCode", messageCode).put("messageCode2", messageCode2).put("phoneNumber", phoneNumber);
    }

    /**
     * 用户重置密码 获取验证码
     */
//    @PostMapping("/userMessageVerify")
//    @ApiOperation(value = "用户重置密码 获取验证码",notes = "" +
//            "phoneNumber: 手机号码 必填\n" +
//            "",httpMethod = "POST")
    public JsonResult userMessageVerify(String phoneNumber, HttpServletRequest request) throws Exception {
        String random = (int)((Math.random()*9+1)*1000) + "";
        PhoneUtils.sendMsg(phoneNumber, "596942", new String[]{random},1);

        // 1. 创建缓存管理器d:\\logFile\\sszg_chf\\
//        CacheManager cacheManager = CacheManager.create("./src/main/resources/ehcache/ehcache-shiro.xml");
       /* CacheManager cacheManager = CacheManager.create("d:/ehcache/ehcache-shiro.xml");
        // 2. 获取缓存对象
        Cache cache = cacheManager.getCache(CacheConst.PHONEMESSAGEVERIFY);
        // 3. 创建元素
        Element element = new Element(phoneNumber, random);
        // 4. 将元素添加到缓存
        cache.put(element);*/

        //新建FIFOCache
        //加入元素，每个元素可以设置其过期时长，DateUnit.SECOND.getMillis()代表每秒对应的毫秒数，在此为秒
        CacheUtils.timedCache.put(phoneNumber, random, DateUnit.SECOND.getMillis() * 300);

        //value1为null
//        String value1 = fifoCache.get(phoneNumber);


//        HttpSession session = request.getSession(true);
//        session.setAttribute(phoneNumber, random);
        return JsonResult.ok("验证码").put("data", random);
    }




}
